package com.mt.mtbletools;

import org.xutils.ex.DbException;
import com.mt.help.GlobalVariable;
import com.mt.model.MixedValues;
import com.mt.model.WXCfgModel;
import com.mt.mtbletools.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;

public class AppCfgActivity extends Activity {
	private GlobalVariable mGlobalVariable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_appcfg);
		mGlobalVariable = GlobalVariable.getinstance();

		initView();
	}

	// 初始化控件
	private Button cfg_btn;
	private View pwd_layout;
	private EditText pwd_edit;
	private View wxacount_layout;
	private EditText wxappid_edit;
	private EditText wxappsecret_edit;
	private EditText productid_edit;
	private Spinner usertype_spinner;
	private static final String USERTYPE_SELCET[] = { "智能用户", "馒头Beacon用户", "馒头模块用户", "馒头微信模块用户", "BLE开发者" };
	private ArrayAdapter<String> usertype_adapter;

	private void initView() {
		cfg_btn = (Button) findViewById(R.id.cfg_btn);
		usertype_spinner = (Spinner) findViewById(R.id.usertype_spinner);

		pwd_layout = (View) findViewById(R.id.pwd_layout);
		pwd_edit = (EditText) findViewById(R.id.pwd_edit);
		wxacount_layout = (View) findViewById(R.id.wxacount_layout);
		wxappid_edit = (EditText) findViewById(R.id.wxappid_edit);
		wxappsecret_edit = (EditText) findViewById(R.id.wxappsecret_edit);
		productid_edit = (EditText) findViewById(R.id.productid_edit);

		usertype_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, USERTYPE_SELCET);
		usertype_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		usertype_spinner.setAdapter(usertype_adapter);

		cfg_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (!wxappid_edit.getText().toString().equals(mGlobalVariable.getMixedvalues().getWxappid())) { // 如果更新了微信账号，则需要删除保存的信息
					try {
						mGlobalVariable.getDb().delete(WXCfgModel.class);
					} catch (DbException e) {
						e.printStackTrace();
					}
				}

				mGlobalVariable.getMixedvalues().setUsermodel(usertype_spinner.getSelectedItemPosition());

				mGlobalVariable.getMixedvalues().setPwd(pwd_edit.getText().toString());
				mGlobalVariable.getMixedvalues().setWxappid(wxappid_edit.getText().toString());
				mGlobalVariable.getMixedvalues().setWxappsecret(wxappsecret_edit.getText().toString());
				mGlobalVariable.getMixedvalues().setProductid(productid_edit.getText().toString());

				try {
					System.out.println("保存配置");
					mGlobalVariable.getDb().saveOrUpdate(mGlobalVariable.getMixedvalues());
					MixedValues mixedvalues = mGlobalVariable.getDb().findFirst(MixedValues.class);
					System.out.println("mixedvalues->" + mixedvalues.getUsermodel());
				} catch (DbException e) {
					e.printStackTrace();
					System.out.println("DbException->" + e.toString());
				}

				finish();
			}
		});

		usertype_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				switch (position) {
				case 0:
					pwd_layout.setVisibility(View.VISIBLE);
					wxacount_layout.setVisibility(View.GONE);
					break;
				case 1:
					pwd_layout.setVisibility(View.VISIBLE);
					wxacount_layout.setVisibility(View.GONE);
					break;
				case 2:
					pwd_layout.setVisibility(View.VISIBLE);
					wxacount_layout.setVisibility(View.GONE);
					break;
				case 3:
					pwd_layout.setVisibility(View.GONE);
					wxacount_layout.setVisibility(View.VISIBLE);
					break;
				case 4:
					pwd_layout.setVisibility(View.GONE);
					wxacount_layout.setVisibility(View.GONE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		usertype_spinner.setSelection(mGlobalVariable.getMixedvalues().getUsermodel());

		pwd_edit.setText(mGlobalVariable.getMixedvalues().getPwd());
		wxappid_edit.setText(mGlobalVariable.getMixedvalues().getWxappid());
		wxappsecret_edit.setText(mGlobalVariable.getMixedvalues().getWxappsecret());
		productid_edit.setText(mGlobalVariable.getMixedvalues().getProductid());
	}

}
